package com.bi.onlineEducation.service;

import com.bi.onlineEducation.dao.CourseDao;
import com.bi.onlineEducation.dao.ExamineDao;
import com.bi.onlineEducation.domain.CoursePo;
import com.bi.onlineEducation.domain.ExaminePo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chenYuTao
 * @Time: 2022/1/19 20:57
 * @Description: 考试业务处理层
 **/
@Service
public class ExamineService {

    @Resource
    private ExamineDao dao;
    @Resource
    private CourseDao courseDao;


    public List<ExaminePo> findAll() {
        List<ExaminePo> daoAll = dao.findAll();
        return daoAll;
    }

    public ExaminePo findById(Integer id) {
        return dao.findById(id).get();
    }

    public ExaminePo save(ExaminePo examinePo) {
        examinePo.setCreateTime(new Date());
        examinePo.setCName(courseDao.findById(examinePo.getCId()).get().getCName());
        return dao.save(examinePo);
    }

    public String updateSelected(ExaminePo examinePo) {
        examinePo.setCreateTime(new Date());
        examinePo.setCName(courseDao.findById(examinePo.getCId()).get().getCName());
        if (!StringUtils.equals(examinePo.getResult1(), examinePo.getSelected1())) {
            return "选择题回答错误";
        } else if (!StringUtils.equals(examinePo.getResult2(), examinePo.getSelected2())) {
            return "判断题回答错误";
        } else if (!StringUtils.equals(examinePo.getResult3(), examinePo.getSelected3())) {
            return "填空题回答错误";
        } else {
            dao.save(examinePo);
            return "回答正确";
        }
    }

    public Boolean deleteById(Integer id) {
        dao.deleteById(id);
        return true;
    }

    @Transactional
    public Boolean deleteByIds(List<Integer> ids) {
        for (Integer id : ids) {
            dao.deleteById(id);
        }
        return true;
    }

    public List<ExaminePo> findByCId(Integer cId) {
        List<ExaminePo> examinePoList = dao.findAllBycId(cId);
        return examinePoList;
    }

    public List<CoursePo> findAllByIsNotExistExamine() {
        List<CoursePo> courseDaoAll = courseDao.findAll();
        List<Integer> examineCIdList = dao.findAll().stream().map(vo -> vo.getCId()).collect(Collectors.toList());
        HashMap<Integer, CoursePo> result = new HashMap<>();
        List<CoursePo> collect = courseDaoAll.stream().filter(vo -> examineCIdList.indexOf(vo.getCId()) <= -1).collect(Collectors.toList());
        return collect;
    }

    /**
     * 考试内容搜索全部
     *
     * @param title
     * @return
     */
    public List<ExaminePo> search(String title) {
        List<ExaminePo> all = dao.findAll();
        ArrayList<ExaminePo> titleList = new ArrayList<>();
        for (ExaminePo examinePo : all) {
            if (StringUtils.isNotBlank(title) && examinePo.getTitle1().indexOf(title) > -1) {
                titleList.add(examinePo);
            }
        }
        if (StringUtils.isBlank(title)) titleList = (ArrayList<ExaminePo>) all;
        return titleList;
    }
}
